<?php
/**
 * ExaminationType Service.
 * User: Administrator
 * Date: 2020/2/13 0013
 * Time: 下午 2:02
 */
namespace app\api\service\questions;

use app\api\model\questions\QuestionCollection as QuestionCollectionModels;

Class QuestionCollection {

    /**
     * 获取题目
     * @param int $aid
     * @return mixed
     */
    public function get( int $aid )
    {
        return QuestionCollectionModels::aid($aid)->findOrEmpty();
    }

    /**
     * 收藏/取消
     * @param int $qid
     * @return mixed
     */
    public function collection(int $aid)
    {
        $model = QuestionCollectionModels::aid($aid)->findOrEmpty();
        if ( false === $model->isEmpty() ) {
            return $model->delete();
        }
        $create = QuestionCollectionModels::create([
           'aid' => $aid
        ]);
        return isset($create->id) && $create->id;
    }

    /**
     * 获取收藏列表
     *
     * @return mixed
     */
    public function lists()
    {
        return QuestionCollectionModels::with(['answer'])->select();
    }

    /**
     * 获取收藏详情
     * @param int $id
     *
     * @return mixed
     */
    public function read(int $id)
    {
        return QuestionCollectionModels::with(['answer'=>function($query){
            $query->with(['opt'])->append(['type_text']);
        }])->getOrFail($id);
    }

}